<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@page import="com.cap.bts.framework.usermgnt.constant.EmployeeStatus"%>    
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定义卡片类型</title>
<%@include file="../include/include.jsp"%>
<link rel="stylesheet" type="text/css" href="${csspath }/public.css"/>
<link rel="stylesheet" type="text/css" href="${csspath }/datepicker.css"/>
<link rel="stylesheet" type="text/css" href="${csspath }/style_l.css"/>
<script type="text/javascript" src="${jspath }/jquery-1.4.4.js"></script>
<script type="text/javascript" src="${jspath }/jquery.ui.min.js"></script>
<script type="text/javascript" src="${jspath }/ui.datepicker_zh-CN.js"></script>
<script type="text/javascript" src="${jspath }/jquery.blockUI.js"></script>
<script type="text/javascript" src="${jspath }/public.js"></script>
<script type="text/javascript" src="${jspath }/jquery.selectAll.js"></script>
<script type="text/javascript" src="${jspath}/jquery/jquery.ui.draggable.js"></script>
<script type="text/javascript" src="${jspath}/jquery/jquery.alerts.js"></script>
<link rel="stylesheet" type="text/css" href="${csspath}/jquery.alerts.css" />

<link rel="stylesheet" type="text/css"
	href="${webcontext}/css/validationEngine.jquery.css" />
<script src="${jspath}/jquery/validate/jquery.validationEngine.js"
	type="text/javascript"></script>
<script src="${jspath}/jquery/validate/jquery.validationEngine-zh.js"
	type="text/javascript"></script>
	<script type="text/javascript"
	src="${webcontext}/javascript/jquery/jquery.ui.draggable.js"></script>
	<link rel="stylesheet" type="text/css"
	href="${bootstrapPath}/css/bootstrap.css" />
<script type="text/javascript" src="${bootstrapPath}/js/bootstrap.js"></script>
<style type="text/css">
.formError .formErrorContent {
    -moz-border-radius: 6px 6px 6px 6px;
    -moz-box-shadow: 0 0 6px #000000;
    background: none repeat scroll 0 0 #EE0101;
    border: 2px solid #DDDDDD;
    color: #FFFFFF;
    font-family: tahoma;
    font-size: 11px;
    padding: 4px 10px;
    width: 100px;
}
</style>
<script type="text/javascript">
var isCardNameExsit;
var nameExsit;
function validate(){
	var errMsg="";
	if($("#name").val()==""){ errMsg="产品名不能为空 \n"}
	if($("#perfix").val()==""){ errMsg+="前缀不能为空\n"}
	//if($("#lengthOfChar").val()==""){errMsg="字母长度不能为空"}
	if($("#lengthOfDigital").val()==""){errMsg+="数字长度不能为空\n"}
	if($("#activationCodeLength").val()==""){errMsg+="卡密长度不能为空\n"}
	//if(isNaN($("#lengthOfChar").val())|| $("#lengthOfChar").val()>4){errMsg="字母长度需为数字,且4位以下\n"}
	if(isNaN($("#lengthOfDigital").val()) || $("#lengthOfDigital").val()<4){errMsg+="数字长度需为数字，且4位或4位以上\n"}
	if(isNaN($("#activationCodeLength").val()) || $("#activationCodeLength").val()<4){errMsg+="卡密长度需为数字，且4位或4位以上\n"}
	return errMsg;
}
function changeStatus(){
	$("#form").submit();
}

function search() {
	$("#form").submit();
}

function addCard(){
	if($("#htxtIsEditMode").val()=="1"){
		jAlert("请先保存未完成操作");
		return
	}else{
		$("#htxtIsEditMode").val("1");
	}
    	var type='<%= CodeTextUtils.getOptions("CardInfo.Type","") %>';
		var pwType='<%= CodeTextUtils.getOptions("CardInfo.PwType","") %>';
		var tr="<tr><td><input type='text' class='form-control' style='width:80px' id='name' name='name'></td>";
		tr+="<td class='align_m'><select id='isPhysicalCard' style='width:90px' class='form-control' name='isPhysicalCard'>"+type+"</select></td>";
		tr+="<td class='align_m'><input class='form-control' type='text' id='perfix' name='perfix'  style='width:50px;' maxlength='3'></td>";
		//tr+="<td class='align_m'><input type='text' id='lengthOfChar' name='lengthOfChar'  style='width:20px;' maxlength='1'></td>";
		tr+="<td class='text-center'><input class='form-control' style='width:35px' type='text' id='lengthOfDigital' name='lengthOfDigital'  maxlength='1'></td>";
		tr+="<td class='text-center'><input class='form-control' type='checkbox' id='checkBitEnabeld' name='checkBitEnabeld' checked></td>";
		tr+="<td class='align_m'></td>";
		tr+="<td class='align_m'><select class='form-control' name='activationCodeType'>"+pwType+"</select></td>";
		tr+="<td class='text-center'><input class='form-control' style='width:35px' type='text' id='activationCodeLength'  name='activationCodeLength'  maxlength='1'><input type='hidden' name='status' value='0'></td>";
		tr+="<td class='align_m'><input class='form-control' type='hidden' id='cardNum' name='cardNum' value=''></td>";
		tr+="<td class='align_m'></td>";
		tr+="<td class='align_m'></td>";
		tr+="<td class='align_m'></td>";
		tr+="<td class='align_m'><a href='javascript:void(0)' onClick='saveCard()'>保存</a>&nbsp;<a href='javascript:void(0)' onClick='cancel(this)'>取消</a></td></tr>"; 
		$('#table tr:eq(0)').after(tr);
}
function cancel(link){
	$("#htxtIsEditMode").val("0");
	$(link).parents("tr").remove();
}

function cancelSave(){
	if($("#checkBitEnabeld").val()=="on"||$("#checkBitEnabeld").val()=="1"){
		$("#checkBitEnabeld").val("1")
	}else{
		$("#checkBitEnabeld").val("0")
	}
	$("#form").submit();
	//window.location.href=window.location.href;
}

function modityCard(id,name,isPhysicalCard,prefix,lengthOfChar,lengthOfDigital,checkBitEnabeld,activationCodeType,activationCodeLength,status){
	if($("#htxtIsEditMode").val()==1){
		jAlert("请先保存未完成操作");
		return;
	}else{
		$("#htxtIsEditMode").val("1");
	}
	var type;
	if(isPhysicalCard==1){type='<%= CodeTextUtils.getOptions("CardInfo.Type","1") %>';}
	else{type='<%= CodeTextUtils.getOptions("CardInfo.Type","0") %>';}
	var pwType
	if(activationCodeType==0){pwType='<%= CodeTextUtils.getOptions("CardInfo.PwType","0") %>';}
	else if(activationCodeType==1){pwType='<%= CodeTextUtils.getOptions("CardInfo.PwType","1") %>';}
	else if(activationCodeType==2){pwType='<%= CodeTextUtils.getOptions("CardInfo.PwType","2") %>';}
	var vstatus;
	if(status==0){vstatus='<%= CodeTextUtils.getOptions("CardInfo.Status1","0") %>';}
	else if(status==1){vstatus='<%= CodeTextUtils.getOptions("CardInfo.Status1","1") %>';}
	else if(status==2){vstatus='<%= CodeTextUtils.getOptions("CardInfo.Status1","2") %>';}
	$("#htxtId").val(id);
	//$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(0).html("<input type='text' id='name' name='name' value="+name+">");
 	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(1).html("<select class='form-control' style='width:90px' name='isPhysicalCard'>"+type+"</select>");
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(2).html("<input type='hidden' id='perfixHtxt'   value="+prefix+"><input class='form-control' style='width:50px' type='text' id='perfix' name='perfix'  style='width:30px;' maxlength='3' value="+prefix+">");
	//$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(3).html("<input type='text' id='lengthOfChar' name='lengthOfChar'  style='width:20px;' maxlength='1' value="+lengthOfChar+">");
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(3).html("<input type='text' class='form-control' style='width:35px' id='lengthOfDigital' name='lengthOfDigital'   maxlength='1' value="+lengthOfDigital+">");
	if(checkBitEnabeld==1){
		$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(4).html("<input class='form-control' style='width:35px' type='checkbox' id='checkBitEnabeld' name='checkBitEnabeld' checked>");
	}else{
		$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(4).html("<input class='form-control' style='width:35px' type='checkbox' id='checkBitEnabeld' name='checkBitEnabeld' >");
	}
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(5).html();
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(6).html("<select class='form-control' name='activationCodeType'>"+pwType+"</select>");
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(7).html("<input class='form-control' style='width:35px' type='text' id='activationCodeLength' name='activationCodeLength' class='form-control'  maxlength='1' value="+activationCodeLength+">");
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(8).html("");
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(9).html("<select class='form-control' style='width:82px' name='status'>"+vstatus+"</select>");
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(10).html("");
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(11).html("");
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(12).html("<a href='javascript:void(0)' onClick='modifyCardSave()'>保存</a>&nbsp;<a href='javascript:void(0)' onClick='cancelSave()'>取消</a>");
   return;
}

function modityCardStatus1(id,status,prefix){
	if($("#htxtIsEditMode").val()==1){
		jAlert("请先保存未完成操作");
		return;
	}else{
		$("#htxtIsEditMode").val("1");
	}
	if(status==0){vstatus='<%= CodeTextUtils.getOptions("CardInfo.Status1","0") %>';}
	else if(status==1){vstatus='<%= CodeTextUtils.getOptions("CardInfo.Status1","1") %>';}
	else if(status==2){vstatus='<%= CodeTextUtils.getOptions("CardInfo.Status1","2") %>';}
	$("#htxtId").val(id);
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(2).html("<input type='hidden' id='perfixHtxt'   value="+prefix+"><input type='text' id='perfix' name='perfix'  class='form-control' style='width:50px' maxlength='3' value="+prefix+">");
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(9).html("<select class='form-control' style='width:82px' name='status'>"+vstatus+"</select>");
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(12).html("<a href='javascript:void(0)' onClick='modifyCardStatusSave()'>保存</a>&nbsp;<a href='javascript:void(0)' onClick='cancelSave()'>取消</a>");
   return;
}

function modityCardStatus2(id,status,prefix){
	if($("#htxtIsEditMode").val()==1){
		jAlert("请先保存未完成操作");
		return;
	}else{
		$("#htxtIsEditMode").val("1");
	}
	if(status==0){vstatus='<%= CodeTextUtils.getOptions("CardInfo.Status2","0") %>';}
	else if(status==1){vstatus='<%= CodeTextUtils.getOptions("CardInfo.Status2","1") %>';}
	else if(status==2){vstatus='<%= CodeTextUtils.getOptions("CardInfo.Status2","2") %>';}
	$("#htxtId").val(id);
	//$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(2).html("test");
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(2).html("<input type='hidden' id='perfixHtxt'   value="+prefix+"><input type='text' id='perfix' name='perfix'  class='form-control' style='width:50px' maxlength='3' value="+prefix+">");
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(9).html("<select class='form-control' style='width:82px' name='status'>"+vstatus+"</select>");
	$("#modifyLink"+id).parent("td").parent("tr").find("td").eq(12).html("<a href='javascript:void(0)' onClick='modifyCardStatusSave()'>保存</a>&nbsp;<a href='javascript:void(0)' onClick='cancelSave()'>取消</a>");
   return;
}

function saveCard(){
	if($("#checkBitEnabeld").val()=="on"||$("#checkBitEnabeld").val()=="1"){
		$("#checkBitEnabeld").val("1")
	}else{
		$("#checkBitEnabeld").val("0")
	}
	var errMsg=validate();
	if(errMsg!=""){jAlert(errMsg);return;}
	
	var submit=true;
	$.ajax({
		type:'post',
		url: '${webcontext}/crmdp/dealer/card/getCardDefinitionCount',
		data : "id="+$("#htxtId").val()+"&name="+$("#name").val(),
		success : function(data) {
			if(data>=1){
				jAlert('该名称卡片已经存在请重新命名!', '提示'); 
			}else{
				$.ajax({
					type:'post',
					url: '${webcontext}/crmdp/dealer/card/getCardDefinitionCount',
					data : "id="+$("#htxtId").val()+"&perfix="+$("#perfix").val(),
					success : function(data) {
						if(data>=1 ){
							jAlert('该前缀已经存在请重新命名!', '提示'); 
						}else{
							$.ajax({
								type:'post',
								url: '${webcontext}/crmdp/dealer/card/checkExsitUsedCardInfo',
								data : "id="+$("#htxtId").val()+"&perfix="+$("#perfix").val(),
								success : function(data) {
									if(data.rtnInfo!=""){
										jConfirm(data.rtnInfo,"提示",function(result){
											if(result){
												$("#cardNum").val(data.rtnPackageNumber);
												$("#htxtIsEditMode").val("0");
												$("#form").attr("action","${webcontext}/crmdp/dealer/card/addCard");
												$("#form").submit();
											}
										});
									}else{
										$("#htxtIsEditMode").val("0");
										$("#form").attr("action","${webcontext}/crmdp/dealer/card/addCard");
										$("#form").submit();
									}
								}
							});
						}
					}
				});
			}
	    }
	}); 	
}

function modifyCardSave(){
	if($("#checkBitEnabeld").val()=="on"||$("#checkBitEnabeld").val()=="1"){
		$("#checkBitEnabeld").val("1")
	}else{
		$("#checkBitEnabeld").val("0")
	}
	var errMsg=validate();
	if(errMsg!=""){jAlert(errMsg);return;}
	$("#htxtIsEditMode").val("0");
	$.ajax({
		type:'post',
		url: '${webcontext}/crmdp/dealer/card/getCardDefinitionCount',
		data : "id="+$("#htxtId").val()+"&perfix="+$("#perfix").val(),
		success : function(data) {
			if(data>=1 && $("#perfix").val()!=$("#perfixHtxt").val()){	
				jAlert('该前缀已经存在请重新命名!', '提示'); 
				return;
	    	}else{
	    			$.ajax({
						type:'post',
						url: '${webcontext}/crmdp/dealer/card/checkExsitUsedCardInfo',
						data : "id="+$("#htxtId").val()+"&perfix="+$("#perfix").val(),
						success : function(data) {
							if(data.rtnInfo!=""){
								jConfirm(data.rtnInfo,"提示",function(result){
									if(result){
										$("#cardNum").val(data.rtnPackageNumber);
							    		$("#form").attr("action","${webcontext}/crmdp/dealer/card/modifyCard");
							    		$("#form").submit();
									}
								});
							}else{
					    		$("#form").attr("action","${webcontext}/crmdp/dealer/card/modifyCard");
					    		$("#form").submit();
							}
						}
					});	
			}
	    	}
		
	});	
}


function isCardNameExsit(){
	$.ajax({
		type:'post',
		url: '${webcontext}/crmdp/dealer/card/isCardNameExsit',
		data : "id="+$("#htxtId").val()+"&name="+$("#name").val(),
		success : function(data) {
			if(data==true){
				nameExsit=$("#name").val();
				jAlert('该名称卡片已经存在请重新命名!'+nameExsit, '提示'); 
				isCardNameExsit=true;
			}else{
				isCardNameExsit=false;
			}
	    }
	}); 
}  

function modifyCardStatusSave(){
	if($("#perfix").val()>$("#perfixHtxt").val()){
		$("#form").attr("action","${webcontext}/crmdp/dealer/card/modifyCard");
		$("#form").submit();
	}else{
		jAlert('输入前缀字符必须大于修改前前缀字符!', '提示'); 
		return;
	}
}


function deleteCard(id){
	$("#htxtId").val(id);
	$("#form").attr("action","${webcontext}/crmdp/dealer/card/deleteCard");
	$("#form").submit();
}

</script> 
</head>
<body>
	<div id="content">
		<div id="wrap">
			<form:form class="form-inline" id="form" action="${webcontext}/crmdp/dealer/card/toDefineCard" method="post" >
				<input type="hidden" id="htxtId" name="id">
				<input type="hidden" id="htxtIsEditMode" value="0">
					<div class="text-center">
					<div class="form-group">
						<label style="padding-top: 0;margin-top: 6px;" class="control-label" for="expressCom">使用状态：</label>
					</div>
					<div class="form-group">
						<select id="selAllStatus" name="queryStatus" class="form-control" style="width:100px" onChange="changeStatus()">  
						  	<option value="">全选择</option>
						  	<core:if test="${QueryStatus==null}">
						  		<%= CodeTextUtils.getOptions("CardInfo.Status","" ) %>
						  	</core:if>
						  	<core:if test="${QueryStatus!=null}">
						  		<%= CodeTextUtils.getOptions("CardInfo.Status",request.getAttribute("QueryStatus").toString()) %>
						  	</core:if>
						 </select>
					</div>
					<div class="form-group" style="margin-left: 100px;">
						<a href="javascript:void(0)" onClick="addCard()" class="btn btn-success" style="width: 150px;">
								<i class="glyphicon glyphicon-plus" style="color:#FFFFFF"></i>&nbsp;添加新卡片类型
						</a>
					</div>
				</div>
				<div class="spliter h_10"></div>
				
				<table id="table" class="table">
					<tr>
						<th width="10%">卡片名称</th>
						<th width="7%">类型</th> 
						<th width="7%">前缀</th>
						<th width="5%">数字长度</th>
						<th width="5%">启动校验</th>
						<th width="5%">卡号长度</th>
						<th width="10%">卡密类型</th>
						<th width="5%">卡密长度</th>
						<th width="10%">最新卡号</th>
						<th width="7%">状态</th>
						<th width="7%">创建日期</th>
						<th width="7%">更新日期</th>
						<th width="8%">操作</th>
					</tr>
					   <core:forEach items="${CardDefineResultsDto.cardDefineList}" var="list">
					   <input type="hidden" id="htxtId${list.id}" value="${list.id}">
					<tr>
						<td class="text-left">${list.name}</td>
						<td class="text-left">
						<core:choose>
						<core:when test="${list.isPhysicalCard==0}">
						<%= CodeTextUtils.getText("CardInfo.Type",0) %>
						</core:when>
						<core:when test="${list.isPhysicalCard==1}">
						<%= CodeTextUtils.getText("CardInfo.Type",1) %>
						</core:when>
						</core:choose>
						</td> 
						<td class="text-left">${list.perfix}</td>
						<%-- <td class="align_m">${list.lengthOfChar}</td> --%>
						<td class="text-center">${list.lengthOfDigital}</td>
						<td class="text-center"><input type="checkbox" <core:if test="${list.checkBitEnabeld==1}">checked</core:if> disabled ></td>
						<td class="text-center">${list.lengthOfChar+list.lengthOfDigital+list.perfix.length()+list.checkBitEnabeld} </td>
						<td class="text-left">
						<core:choose>
						<core:when test="${list.activationCodeType==0}">
							<%= CodeTextUtils.getText("CardInfo.PwType",0) %>
						</core:when>
						<core:when test="${list.activationCodeType==1}">
							<%= CodeTextUtils.getText("CardInfo.PwType",1) %>
						</core:when>
						<core:when test="${list.activationCodeType==2}">
							<%= CodeTextUtils.getText("CardInfo.PwType",2) %>
						</core:when>
						</core:choose>
						</td>
						<td class="text-center">${list.activationCodeLength}</td>
						<td class="text-left">${list.cardNum}
						<%-- ${list.perfix}<core:if test="${list.lengthOfChar!=0 or list.lengthOfChar!=null}"><core:forEach var="s" begin="1" end="${list.lengthOfChar}">A</core:forEach></core:if><core:if test="${list.lengthOfDigital!=0 or list.lengthOfDigital!=null}"><core:forEach var="s" begin="1" end="${list.lengthOfDigital}">0</core:forEach></core:if><core:if test="${list.checkBitEnabeld==1}">X</core:if> --%>
						</td>
						<td class="text-left">
						<core:choose>
						<core:when test="${list.status==0}">
						<%= CodeTextUtils.getText("CardInfo.Status",0) %>
						</core:when>
						<core:when test="${list.status==1}">
						<%= CodeTextUtils.getText("CardInfo.Status",1) %>
						</core:when>
						<core:when test="${list.status==2}">
						<%= CodeTextUtils.getText("CardInfo.Status",2) %>
						</core:when>
						</core:choose>
						</td>
						<td class="text-left">
						<fmt:formatDate value="${list.createDate}" pattern="yyyy-MM-dd HH:mm:ss" var="createDate"/>
						${fn:substring(createDate, 0, 10)}
						</td>
						<td class="text-left">
						<fmt:formatDate value="${list.lastUpdateDate}" pattern="yyyy-MM-dd HH:mm:ss" var="lastUpdateDate"/>
						${fn:substring(lastUpdateDate, 0, 10)}
						
						</td>
						<td class="text-left">
						<core:choose>
							<core:when test="${list.status==0}">
								<a id="modifyLink${list.id}" href="javascript:void(0)" onClick="modityCard('${list.id}','${list.name}','${list.isPhysicalCard}','${list.perfix}','${list.lengthOfChar}','${list.lengthOfDigital}','${list.checkBitEnabeld}','${list.activationCodeType}','${list.activationCodeLength}','${list.status}')">编辑</a>
								<a href="javascript:void(0)" onClick="deleteCard('${list.id}')">删除</a>
							</core:when>
							<core:when test="${list.status==1 or list.status==2}">
								<a id="modifyLink${list.id}" href="javascript:void(0)" onClick="modityCardStatus2('${list.id}','${list.status}','${list.perfix}')">编辑 </a>
							</core:when>
						</core:choose>
						</td>
				</tr>
			</core:forEach>
			</table>
		<div class="page_div">
		    <%@include file="../common/page.jsp"%>
		</div>
        </form:form>
        </div>
        </div>
</body>
</html>